package com.cssiot.weixin.basic.util;
import java.io.File;  
import java.io.FileInputStream;  
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.security.KeyStore;  
  
import javax.net.ssl.SSLContext;  
  
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;  
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;  
import org.apache.http.ssl.SSLContexts;  
import org.apache.http.util.EntityUtils;  
  
public class HttpsDemo {  
    private final static String PFX_PATH = "e:/HttpsDemo/client.p12";   //客户端证书路径  
    private final static String PFX_PWD = "123456"; //客户端证书密码  
    //关于http 需要两个jar包   httpclient-4.0.jar	httpcore-4.0.1.jar
    private static final HttpClient httpClient = new DefaultHttpClient();
      
    public static String sslRequestGet(String url) throws Exception {  
        KeyStore keyStore = KeyStore.getInstance("PKCS12");  
        InputStream instream = new FileInputStream(new File(PFX_PATH));  
        try {  
            keyStore.load(instream, PFX_PWD.toCharArray());  
        } finally {  
            instream.close();  
        }  
  
        SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, PFX_PWD.toCharArray()).build();  
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext  
                , new String[] { "TLSv1" }  // supportedProtocols ,这里可以按需要设置  
                , null  // supportedCipherSuites  
                , SSLConnectionSocketFactory.getDefaultHostnameVerifier());   
          
        CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();  
        try {  
            HttpGet httpget = new HttpGet(url);   
//          httpost.addHeader("Connection", "keep-alive");// 设置一些heander等  
            CloseableHttpResponse response = httpclient.execute(httpget);  
            try {  
                HttpEntity entity = response.getEntity();  
                String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");//返回结果  
                EntityUtils.consume(entity);  
                return jsonStr;  
            } finally {  
                response.close();  
            }  
        } finally {  
            httpclient.close();  
        }  
    }  
  
    public static void main(String[] args) throws Exception {  
        System.out.println(System.getProperty("java.home"));  
        System.out.println(sslRequestGet("https://www.ttt.com/"));  
    }  
    
    public static void request() throws  Exception{
    	try {
			//获得密匙库
		KeyStore trustStore = KeyStore.getInstance("jks");
		String keyStoreFile = "xxxxx.keystore";
	        String keyPwd =  "xxxxxxx";
		FileInputStream instream = new FileInputStream(new File(keyStoreFile));
	        //密匙库的密码
	        trustStore.load(instream, keyPwd.toCharArray());
	        //注册密匙库
	        SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
	        //不校验域名
	        socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
	        Scheme sch = new Scheme("https", socketFactory, 443);
	        httpClient.getConnectionManager().getSchemeRegistry().register(sch);
		} catch (Exception e) {
			e.printStackTrace();
				}
		//下面这段是调用代码，可以有很多种写法，不局限于用HttpPost
		HttpPost httpPost = new HttpPost( "" );
		StringEntity entity = new StringEntity("");
		entity.setContentEncoding("UTF-8");
		httpPost.setEntity( entity );
		//发送请求
		HttpResponse response = httpClient.execute( httpPost );
		String jsonStr = EntityUtils.toString( response.getEntity() );

    }


    	


}  